﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AutoEditUI.aspx.cs" Inherits="iSun.WebApp.AutoEditUI"
    EnableViewState="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
     <style type="text/css">
       
        textarea 
        {
            height:100px;
        }
        
        label
        {
            font-size:14px;
            text-align:right;
            width:200px;
        }
       body
       {
            background: #eeeeee;
       }
    </style>
    <link rel="stylesheet" type="text/css" href="../Styles/default/calendar.css" />
    <link href="../Styles/base.css" rel="stylesheet" type="text/css" />
    <link href="../Styles/formReForm.css" rel="stylesheet" type="text/css" />

    <script src="../Scripts/jquery-1.6.min.js" type="text/javascript"></script>
    <script src="../Scripts/jquery.json-2.2.js" type="text/javascript"></script>
    <script src="../Scripts/Jquery.form.js" type="text/javascript"></script>
    <script src="../Scripts/jquery.calendar.js" type="text/javascript"></script>
    <script src="../Scripts/jquery.upload.js" type="text/javascript"></script>
    <script src="../Scripts/CombinButton.js" type="text/javascript"></script>
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/json/json-min.js"></script>
    <script type="text/javascript" src="../Scripts/yui_2.9.0/build/connection/connection-min.js"></script>
    <script src="../Scripts/formReForm.js" type="text/javascript"></script>
    <script src="../Scripts/Appjs/CheckModel.js" type="text/javascript"></script>

</head>
<body >
    <form id="form1" runat="server">
    <div class="fieldForm" id="fieldForm" >
        <asp:Literal ID="FormTemplate" runat="server"></asp:Literal>
    </div>
    </form>
    <form id="form2" action="AjaxData/DataSave.aspx">
    <textarea style="display: none" id="jsonDataContent" rows="2" cols="2" name="jsonDataContent"></textarea>
    </form>
    <p>
</body>
<script type="text/javascript">
        var _domain = this.location.host;
        //var _href =  _domain.split('/');
        //alert(_href);
        //window.open();
        
        var url = "http://"+_domain + "/Extend/AjaxData/DataSave.aspx",
        entity = "<% =EntityName %>";
        var Parent = parent;
        //alert(url);
        <asp:Literal ID="EditTemplate" runat="server"></asp:Literal>
        <asp:Literal ID="MasterID" runat="server"></asp:Literal>
        //alert(instance[0].EntityName);
        
        ///-------------------------------------
        ///供调用方法
        ///-------------------------------------
        function Save(){
            if(checkInputData()=="0")
             {
                   return;
             }

            if(typeof(fnCustomSave)=="function")
            {
                fnCustomSave();
                return ;
            }

            if(typeof(trySave)=="function")
            {
                setValue();
                var json =$.toJSON(instance[0]).replace(/'/g,"[B]");
                //var json =$.toJSON(instance[0]).replace(/'/g,"[B]").replace(/\n/g,'[BR]');
                $("#jsonDataContent").val(json);
                trySave();
                return;
            }
            fnSystemSave();
        }

        ///------------------------------------
        //系统的保存方法
        ///------------------------------------
        var action = '<% =DataAction %>';
        function setValue()
        {
           <asp:Literal ID="jSave" runat="server"></asp:Literal>
           //instance[0]= $("form1").extractObject(); 
        }
        function fnSystemSave()
        {
           
            ///初始化相关参数
            setValue();
            $("#jsonDataContent").val($.toJSON(instance[0]));
            var SaveUrl =url+"?r=" + Math.random() + "&entity=" + entity + "&action="+action+"&json=" + encodeURI($("#jsonDataContent").val().replace(/&/g,"[A]").replace(/'/g,"[B]"));
             if(typeof(checkData)=="function")
            {
                 if(checkData()=="0")
                 {
                     return;
                 }
             }
            
            ///调用验证
            if(typeof(fnCheckData)=="function")
            {
                var msg =fnCheckData(instance[0]);
                if(!msg=="")
                {
                    alert(msg);
                    return;
                }
            }
            //发送保存请求
            YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
                success: responseSuccess
                ,
                failure: responseFailure
            }, null);
        }
       
       ///初始化相关信息
       $(document).ready(function() {
           <asp:Literal ID="jInitControl" runat="server"></asp:Literal>
           ///编辑状态
           if(getPar1("id")!="")
           {
               $("#form1").JSONToForm($.toJSON(instance[0]));
           }
           showMessage();
        });
         
         var formReForm;
         function doReform() {
            // load the css
            formReForm = new FormReForm('fieldForm');
            //formReForm.loadformReFormCss();

            // do the reform with default settings
            formReForm.doReForm();

        }
        
        
        var trySave = function()   
        {
            
            // argument formId can be the id or name attribute value of the
            // HTML form, or an HTML form object.
            var formObject = document.getElementById('form2');
            YAHOO.util.Connect.setForm(formObject);
            // This example facilitates a POST transaction.  The POST data(HTML form)
            // are initialized when calling setForm(), and it is automatically
            // included when calling asyncRequest.
            var cObj = YAHOO.util.Connect.asyncRequest('POST',url+"?flag=1&action="+action+"&entity="+entity,callback);
        }


        var responseSuccess = function (res) {
                    try {                    
                        var str = res.responseText;
                        //var obj = eval(str);
                        if(str=="1"){
                            alert("数据保存成功");
                            if(action=='insert'){
                                //parent.fnAddRow(instance[0]);
                                parent.ReLoadData();
                            }else{
                                parent.ReLoadData();
                               //parent.fnUpdateRow(instance[0]);
                            }
                            parent.closeWin();
                        }
                        else
                        {
                            if(str!=""){
                                showMessage();
                                alert(str+"\n"+"数据保存失败!");
                            }
                        }
                    }
                    catch (e) {
                    }
                    finally {
                    }
        };
 
        var responseFailure =function (error) {
                    alert("数据错误：" + error.statusText);
        }
 
        // Create a callback object that defines success and failure
        // handlers, and instructs Connection Manager to use the
        // Flash transport by defining the xdr property and setting
        // it to true.
       var  callback = {
            success:responseSuccess,
            failure:responseFailure
        };
 
       var SetControlValue=function(o)
       {
            $.each(o, function (property, value) {
            //try {
            value = value + "";
            // find the control and set value
            if (property != "") {
                var control = $("[name='" + property + "']");
                var strValue = value.replace(/@1@1@1/g, "'");
                strValue = strValue.replace(/@2@2@2/g, '"');
                strValue = strValue.replace(/@@@@/g, "\n");
                strValue = strValue.replace(/[BR]/g, "\n");
                control.val(strValue);
                try {
                    $('#' + property).combobox('setValue', strValue);
                } catch (Error) { }
            }
            //  };
        });
       }
</script>


<script src="../Scripts/appjs/model.<% =EntityName %>.js" type="text/javascript" language="javascript"></script>
<script type="text/javascript" language="javascript">
    //调用初始化控件功能
    if (typeof (fnInitControl) == "function") {
        fnInitControl();
    }
</script>
</html>
